package com.hzncc.flowable_diboot.contract.delegate;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.flowable_diboot.contract.entity.ContractAudit;
import com.hzncc.flowable_diboot.contract.service.ContractAuditService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * @Description :
 * @Date : 2024/7/26
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Service
public class CheckContractStatusDelegate implements JavaDelegate {
    
    @Resource
    private RuntimeService runtimeService;
    
    @Resource
    private ContractAuditService contractAuditService;
    
    @Override
    public void execute(DelegateExecution delegateExecution) {
        //获取当前任务节点所在流程实例ID
        String processInstanceId = delegateExecution.getProcessInstanceId();
        //查询当前合同状态
        LambdaQueryWrapper<ContractAudit> contractAuditLambdaQueryWrapper = new LambdaQueryWrapper<>();
        contractAuditLambdaQueryWrapper.eq(ContractAudit::getInstanceId,processInstanceId).eq(ContractAudit::isDeleted,0);
        ContractAudit contractAudit = contractAuditService.getSingleEntity(contractAuditLambdaQueryWrapper);
        String contractStatus = contractAudit.getContractStatus();
        //变更流程变量-合同状态值
        runtimeService.setVariable(processInstanceId,"contractStatus",contractStatus);
    }
}
